package ie.dkit.semanticPhotoWeb.client.panels.photo;

import java.util.List;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.dtos.RelationDTO;
import ie.dkit.semanticPhotoWeb.client.panels.CenterPanel;
import ie.dkit.semanticPhotoWeb.client.tools.AsyncCallbackAdapter;
import ie.dkit.semanticPhotoWeb.client.tools.ImageHyperlink;
import ie.dkit.semanticPhotoWeb.client.tools.Refreshable;

import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.TabPanel;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.layout.FitLayout;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class RelationPanel extends Panel implements Refreshable {

	static final int REL_ATTRIBUTE = 0;

	static final int REV_ATTRIBUTE = 1;

	private class ImageIconListener implements ClickListener {

		private ImageDTO imageDTO;

		public ImageIconListener(ImageDTO imageDTO) {
			this.imageDTO = imageDTO;
		}

		public void onClick(Widget sender) {
			m_CenterPanel.addPhotoPanel(imageDTO);
		}

	}

	private class ImageIcon extends Panel {
		private RelationDTO m_CurrentRelationDTO;

		public ImageIcon(RelationDTO dto, int attributeType) {
			m_CurrentRelationDTO = dto;
			ImageHyperlink link = new ImageHyperlink(new Image(
					dto.thumbnails[1]));
			if (attributeType == REL_ATTRIBUTE) {
				link.setRelElement(dto.relationTyp);
			} else if (attributeType == REV_ATTRIBUTE) {
				link.setRevElement(dto.relationTyp);
			}
			link.addClickListener(new ImageIconListener(dto));
			setTitle(dto.relationTyp);
			add(link);
			Button deleteButton = new Button("Delete",
					new ButtonListenerAdapter() {
						public void onClick(Button button, EventObject e) {
							m_CSC.deleteRelation(m_CurrentRelationDTO,
									new AsyncCallbackAdapter() {

										public void onSuccess(Object result) {
											MessageBox
													.alert("Successful delted");
											showRelations();
										}

									});
						}
					});
			deleteButton.setTooltip("Delete this Relation");
			add(deleteButton);
		}
	}

	private Panel m_FromPicturesPanel, m_ToPicturesPanel;

	private ClientServerConnectorAsync m_CSC;

	private ImageDTO m_ImageDTO;

	private CenterPanel m_CenterPanel;

	private TabPanel m_TabPanel;

	protected RelationPanel(
			ClientServerConnectorAsync clientServerConnectorAsync,
			ImageDTO imageDTO, CenterPanel centerPanel) {
		m_CSC = clientServerConnectorAsync;
		m_ImageDTO = imageDTO;
		m_CenterPanel = centerPanel;
		setTitle("Relations");
		setCollapsible(true);
		setWidth(200);
		setLayout(new FitLayout());
		m_TabPanel = new TabPanel();
		m_TabPanel.add(m_ToPicturesPanel = new Panel("Linked to (0)"));
		m_ToPicturesPanel.setAutoScroll(true);
		m_TabPanel.add(m_FromPicturesPanel = new Panel("Linked from (0)"));
		m_FromPicturesPanel.setAutoScroll(true);
		add(m_TabPanel);
		showRelations();
	}

	protected final void showRelations() {
		m_CSC.getLinkedPictures(m_ImageDTO, new AsyncCallbackAdapter() {
			public void onSuccess(Object result) {
				List dtos = (List) result;
				if (dtos != null) {
					m_ToPicturesPanel.clear();
					m_ToPicturesPanel.setTitle("Linked to (" + dtos.size()
							+ ")");
					for (int i = 0; i < dtos.size(); i++) {
						m_ToPicturesPanel.add(new ImageIcon((RelationDTO) dtos
								.get(i), REL_ATTRIBUTE));
					}
					m_TabPanel.add(m_ToPicturesPanel);
					m_TabPanel.doLayout();
				}
			}
		});
		m_CSC.getReferringPictures(m_ImageDTO, new AsyncCallbackAdapter() {

			public void onSuccess(Object result) {
				List dtos = (List) result;
				if (dtos != null) {
					m_FromPicturesPanel.clear();
					m_FromPicturesPanel.setTitle("Linked from (" + dtos.size()
							+ ")");
					for (int i = 0; i < dtos.size(); i++) {
						m_FromPicturesPanel.add(new ImageIcon(
								(RelationDTO) dtos.get(i), REV_ATTRIBUTE));
					}
					m_TabPanel.add(m_FromPicturesPanel);
					m_TabPanel.doLayout();
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.Refreshable#refresh()
	 */
	public void refresh() {
		showRelations();
	}
}
